
package com.buss.core.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.dao.RoleDao;
import com.buss.core.dao.UserDao;
import com.buss.core.model.Role;
import com.buss.core.model.User;
import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.PersistenceCond;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;

/** * 
 * @author gykl
 *
 */
@Repository
public class UserDaoImpl implements UserDao {

	private static final long serialVersionUID = 2398644829905697262L;

	@Autowired
	private BaseDao<User> baseDao;
	
	public void deleteUser(Long[] userIds) {
		String hql = "delete from User user where user.id in (:userIds)";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userIds", userIds);
		baseDao.excuteHsql(hql, map);
	}

	
	public void editUser(User adminUser) {
		String hql = "update User user set user.userId = :userId,user.companyId = :companyId, " +
				"user.userName = :userName, user.email = :email, user.telephone = :telephone, " +
				"user.mobilePhone = :mobilePhone, user.disabled = :disabled, user.sysAdmin = :sysAdmin " +
				"where user.id = :id";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", adminUser.getUserId());
		params.put("companyId", adminUser.getCompanyId());
		params.put("userName", adminUser.getUserName());
		params.put("email", adminUser.getEmail());
		params.put("telephone", adminUser.getTelephone());
		params.put("mobilePhone", adminUser.getMobilePhone());
		params.put("disabled", adminUser.isDisabled());
		params.put("sysAdmin", adminUser.isSysAdmin());
		params.put("id", adminUser.getId());
		baseDao.excuteHsql(hql, params);
	}

	public void editUserPass(Long userId,String password){
		String sql = "update User user set user.userPass = :password where user.id = :userId";
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("password", password);
		baseDao.excuteHsql(sql, params);
	}
	
	public void editOrganTransfer(Long[] userIds,Long organId){
		String hql = "update User user set user.organ.id = :organId where user.id in (:userIds)";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("organId", organId);
		map.put("userIds", userIds);
		baseDao.excuteHsql(hql, map);
	}
	
	public void editRoleToUserId(Long userId,Long[] roleIds){
		String sql = "delete from core_role_user where userId = :userId";
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		baseDao.excuteNative(sql, params);
		if(roleIds == null) return;
		for(Long roleId : roleIds){
			sql = "insert into core_role_user(userId,roleId) values(:userId,:roleId)";
			params = new HashMap<String, Object>();
			params.put("userId", userId);
			params.put("roleId", roleId);
			baseDao.excuteNative(sql, params);
		}
	}
	
	public User loadUser(Long userId) {
		return baseDao.get(userId, User.class);
	}

	
	public ResultList<User> queryUser(String companyId, Long organId,String userName,
			Boolean disabled, Paginalbe page) {
		String sql = "select adminUser from User adminUser where adminUser.companyId=:companyId ";
		PersistenceCond cond = new PersistenceCond();
		cond.add("companyId", companyId);
		if(organId != null){
			sql += " and adminUser.organ.id = :organId ";
			cond.add("organId", organId);
		}
		if(StringUtil.isNotEmpty(userName)){
			sql += " and adminUser.userName like :userName order adminUser.id like :userName ";
			cond.add("userName", "%" + userName.trim() + "%");
		}
		if(disabled != null){
			sql += " and adminUser.disabled = :disabled ";
			cond.add("disabled", disabled);
		}
		return baseDao.queryHsql(sql, cond, page);
	}
	
	public ResultList<User> queryUserBySales(String companyId,Boolean isSalesMan){
		String sql = "select user from User user where user.isSalesMan = :isSalesMan and user.companyId = :companyId and user.disabled=:disabled";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("isSalesMan", isSalesMan);
		params.put("disabled", false);
		return baseDao.queryHsql(sql, params, null);
	}

	public ResultList<User> queryUserByRoleId(String companyId,Long roleId,String userName,
			Boolean disabled,Paginalbe page){
		String sql = "select adminUser from User adminUser inner join adminUser.roles roles where adminUser.companyId = :companyId and roles.id = :roleId";		
		PersistenceCond cond = new PersistenceCond();
		cond.add("companyId", companyId);
		cond.add("roleId", roleId);
		if(StringUtil.isNotEmpty(userName)){
			sql += " and adminUser.userName like :userName ";
			cond.add("userName", "%" + userName.trim() + "%");
		}
		if(disabled != null){
			sql += " and adminUser.disabled = :disabled ";
			cond.add("disabled", disabled);
		}
		return baseDao.queryHsql(sql, cond, page);	
	}

	public void saveUser(User adminUser) {
		baseDao.save(adminUser);

	}

	public void updateUsersRole(Long[] userIds,Long roleId){
		String sql = "select count(*) from core_role_user where userId = :userId and roleId = :roleId ";
		for(Long userId : userIds){
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", userId);
			params.put("roleId", roleId);
			int cnt = Integer.parseInt(baseDao.loadNativeSingleValue(sql, params).toString());
			if(cnt > 0){
				continue;
			}
			baseDao.excuteNative("insert into core_role_user(userId,roleId) values(:userId,:roleId) ", params);
		}
	}
	
	public void removeUsersRole(Long[] userIds,Long roleId){
		String sql = "delete from core_role_user where roleId = :roleId and userId in (:userIds)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("roleId", roleId);
		params.put("userIds", userIds);
		baseDao.excuteNative(sql, params);
	}
	
	public User loadUser(String companyId,String userId){
		String sql = "select user from User user where user.userId = :userId and user.companyId=:companyId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("companyId", companyId);
		return baseDao.loadHsql(sql, params);
	}
}
